package org.jeecg.modules.communityDynamicManag.controller;

import java.util.Arrays;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.SecurityUtils;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.system.vo.LoginUser;
import org.jeecg.common.util.UUIDGenerator;
import org.jeecg.modules.communityDynamicManag.entity.*;
import org.jeecg.modules.communityDynamicManag.mapper.CommunityDynamicManagMapper;
import org.jeecg.modules.communityDynamicManag.service.ICommunityDynamicManagService;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;

import org.jeecg.common.system.base.controller.JeecgController;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.jeecg.common.aspect.annotation.AutoLog;

/**
 * @Description: 社团动态管理
 * @Author: jeecg-boot
 * @Date: 2020-07-21
 * @Version: V1.0
 */
@Api(tags = "社团动态管理")
@RestController
@RequestMapping("/communityDynamicManag/communityDynamicManag")
@Slf4j
public class CommunityDynamicManagController extends JeecgController<CommunityDynamicManag, ICommunityDynamicManagService> {
    @Autowired
    private ICommunityDynamicManagService communityDynamicManagService;

    @Autowired
    private CommunityDynamicManagMapper communityDynamicManagMapper;

    /**
     * 分页列表查询
     *
     * @param communityDynamicManag
     * @param pageNo
     * @param pageSize
     * @param req
     * @return
     */
    @AutoLog(value = "社团动态管理-分页列表查询")
    @ApiOperation(value = "社团动态管理-分页列表查询", notes = "社团动态管理-分页列表查询")
    @GetMapping(value = "/list")
    public Result<?> queryPageList(CommunityDynamicManagResult communityDynamicManag,
                                   @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                   @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                                   HttpServletRequest req) {
        Result<Page<CommunityDynamicManagResult>> result = new Result<Page<CommunityDynamicManagResult>>();
        Page<CommunityDynamicManagResult> pageList = new Page<CommunityDynamicManagResult>(pageNo, pageSize);
        pageList = communityDynamicManagService.getPageList(pageList, communityDynamicManag);
        result.setSuccess(true);
        result.setResult(pageList);
        return result;
    }

    /**
     * 添加
     *
     * @param communityDynamicManag
     * @return
     */
    @AutoLog(value = "社团动态管理-添加")
    @ApiOperation(value = "社团动态管理-添加", notes = "社团动态管理-添加")
    @PostMapping(value = "/add")
    public Result<?> add(@RequestBody CommunityDynamicManag communityDynamicManag) {
        communityDynamicManag.setId(UUIDGenerator.generate());
        // TODO 暂时使用后台登陆用户信息，后期手机登陆与后台登陆用户要区别开
        LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
        communityDynamicManag.setUserId(sysUser.getId());
        communityDynamicManag.setCommunityId("1287683832279023618");
        Boolean bool = communityDynamicManagService.save(communityDynamicManag);
        System.out.println(bool);
        return Result.ok("添加成功！");
    }

    /**
     * 编辑
     *
     * @param communityDynamicManag
     * @return
     */
    @AutoLog(value = "社团动态管理-编辑")
    @ApiOperation(value = "社团动态管理-编辑", notes = "社团动态管理-编辑")
    @PutMapping(value = "/edit")
    public Result<?> edit(@RequestBody CommunityDynamicManag communityDynamicManag) {
        communityDynamicManagService.updateById(communityDynamicManag);
        return Result.ok("编辑成功!");
    }

    /**
     * 通过id删除
     *
     * @param id
     * @return
     */
    @AutoLog(value = "社团动态管理-通过id删除")
    @ApiOperation(value = "社团动态管理-通过id删除", notes = "社团动态管理-通过id删除")
    @DeleteMapping(value = "/delete")
    public Result<?> delete(@RequestParam(name = "id", required = true) String id) {
        communityDynamicManagService.removeById(id);
        return Result.ok("删除成功!");
    }

    /**
     * 批量删除
     *
     * @param ids
     * @return
     */
    @AutoLog(value = "社团动态管理-批量删除")
    @ApiOperation(value = "社团动态管理-批量删除", notes = "社团动态管理-批量删除")
    @DeleteMapping(value = "/deleteBatch")
    public Result<?> deleteBatch(@RequestParam(name = "ids", required = true) String ids) {
        this.communityDynamicManagService.removeByIds(Arrays.asList(ids.split(",")));
        return Result.ok("批量删除成功!");
    }

    /**
     * 通过id查询
     *
     * @param id
     * @return
     */
    @AutoLog(value = "社团动态管理-通过id查询")
    @ApiOperation(value = "社团动态管理-通过id查询", notes = "社团动态管理-通过id查询")
    @GetMapping(value = "/queryById")
    public Result<?> queryById(@RequestParam(name = "id", required = true) String id) {
        CommunityDynamicManag communityDynamicManag = communityDynamicManagService.getById(id);
        if (communityDynamicManag == null) {
            return Result.error("未找到对应数据");
        }
        return Result.ok(communityDynamicManag);
    }

    /**
     * 导出excel
     *
     * @param request
     * @param communityDynamicManag
     */
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, CommunityDynamicManag communityDynamicManag) {
        return super.exportXls(request, communityDynamicManag, CommunityDynamicManag.class, "社团动态管理");
    }

    /**
     * 通过excel导入数据
     *
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, CommunityDynamicManag.class);
    }

    /**
     * 通过id删除
     *
     * @param id
     * @return
     */
    @AutoLog(value = "社团动态管理-通过id删除")
    @ApiOperation(value = "社团动态管理-通过id删除", notes = "社团动态管理-通过id删除")
    @GetMapping(value = "/deleteById")
    public Result<?> deleteById(@RequestParam(name = "id", required = true) String id) {
        communityDynamicManagMapper.removeById(id);
        return Result.ok("删除成功!");
    }

    /**
     * 点赞
     *
     * @param form
     * @return
     */
    @AutoLog(value = "当前动态点赞")
    @ApiOperation(value = "当前动态点赞", notes = "当前动态点赞")
    @PostMapping(value = "/likes")
    public Result<?> likes(@RequestBody ClubDynamicsLikesEntity form) {
        Boolean bool = communityDynamicManagService.saveLikes(form);
        return Result.ok("成功");
    }

    @AutoLog(value = "获取点赞用户")
    @ApiOperation(value = "获取点赞用户", notes = "获取点赞用户")
    @PostMapping(value = "/likeUser")
    public Result<?> likesUser(
            @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
            @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize) {
        //TODO 获取点赞用户列表暂时使用后台用户管理表，等开始做前端后台开发时换成前端等用户管理表
        Result<Page<ClubDynamicsLikesEntityResult>> result = new Result<Page<ClubDynamicsLikesEntityResult>>();
        Page<ClubDynamicsLikesEntityResult> pageList = new Page<>(pageNo, pageSize);
        pageList = communityDynamicManagService.getLikesUser(pageList);
        result.setSuccess(true);
        result.setResult(pageList);
        return result;
    }

    /**
     * 社团动态审核
     * @param id
     * @return
     */
    @AutoLog(value = "动态审核")
    @ApiOperation(value = "动态审核", notes = "动态审核")
    @GetMapping(value = "/dynamicReview")
    public Result<?> dynamicReview(@RequestParam(name = "id", required = true) String id){
        // 根据ID获取发布的社团动态信息
        CommunityDynamicManag form = communityDynamicManagService.getById(id);
        form.setAudit("2");
        Boolean bool = communityDynamicManagService.dynamicReview(form);
        if (bool) {
            return Result.ok("深刻成功");
        } else {
            return Result.error("失败");
        }
    }

}
